{
  "cells": [
    {
      "cell_type": "raw",
      "id": "1957f5cb",
      "metadata": {
        "vscode": {
          "languageId": "raw"
        }
      },
      "source": [
        "---\n",
        "sidebar_label: Upstash Vector\n",
        "---"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "ef1f0986",
      "metadata": {},
      "source": [
        "# UpstashVectorStore\n",
        "\n",
        "[Upstash Vector](https://upstash.com/) is a REST based serverless vector database, designed for working with vector embeddings.\n",
        "\n",
        "This guide provides a quick overview for getting started with Upstash [vector stores](/docs/concepts/#vectorstores). For detailed documentation of all `UpstashVectorStore` features and configurations head to the [API reference](https://api.js.langchain.com/classes/langchain_community_vectorstores_upstash.UpstashVectorStore.html)."
      ]
    },
    {
      "cell_type": "markdown",
      "id": "c824838d",
      "metadata": {},
      "source": [
        "## Overview\n",
        "\n",
        "### Integration details\n",
        "\n",
        "| Class | Package | [PY support](https://python.langchain.com/docs/integrations/vectorstores/upstash/) |  Package latest |\n",
        "| :--- | :--- | :---: | :---: |\n",
        "| [`UpstashVectorStore`](https://api.js.langchain.com/classes/langchain_community_vectorstores_upstash.UpstashVectorStore.html) | [`@langchain/community`](https://npmjs.com/@langchain/community) | ✅ |  ![NPM - Version](https://img.shields.io/npm/v/@langchain/community?style=flat-square&label=%20&) |"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "36fdc060",
      "metadata": {},
      "source": [
        "## Setup\n",
        "\n",
        "To use Upstash vector stores, you'll need to create an Upstash account, create an index, and install the `@langchain/community` integration package. You'll also need to install the [`@upstash/vector`](https://www.npmjs.com/package/@upstash/vector) package as a peer dependency.\n",
        "\n",
        "This guide will also use [OpenAI embeddings](/docs/integrations/text_embedding/openai), which require you to install the `@langchain/openai` integration package. You can also use [other supported embeddings models](/docs/integrations/text_embedding) if you wish.\n",
        "\n",
        "```{=mdx}\n",
        "import IntegrationInstallTooltip from \"@mdx_components/integration_install_tooltip.mdx\";\n",
        "import Npm2Yarn from \"@theme/Npm2Yarn\";\n",
        "\n",
        "<IntegrationInstallTooltip></IntegrationInstallTooltip>\n",
        "\n",
        "<Npm2Yarn>\n",
        "  @langchain/community @langchain/core @upstash/vector @langchain/openai\n",
        "</Npm2Yarn>\n",
        "```\n",
        "\n",
        "You can create an index from the [Upstash Console](https://console.upstash.com/login). For further reference, see [the official docs](https://upstash.com/docs/vector/overall/getstarted).\n",
        "\n",
        "Upstash vector also has built in embedding support. Which means you can use it directly without the need for an additional embedding model. Check the [embedding models documentation](https://upstash.com/docs/vector/features/embeddingmodels) for more details.\n",
        "\n",
        "```{=mdx}\n",
        ":::note\n",
        "To use the built-in Upstash embeddings, you'll need to select an embedding model when creating the index.\n",
        ":::\n",
        "```\n",
        "\n",
        "### Credentials\n",
        "\n",
        "Once you've set up an index, set the following environment variables:\n",
        "\n",
        "```typescript\n",
        "process.env.UPSTASH_VECTOR_REST_URL = \"your-rest-url\";\n",
        "process.env.UPSTASH_VECTOR_REST_TOKEN = \"your-rest-token\";\n",
        "```\n",
        "\n",
        "If you are using OpenAI embeddings for this guide, you'll need to set your OpenAI key as well:\n",
        "\n",
        "```typescript\n",
        "process.env.OPENAI_API_KEY = \"YOUR_API_KEY\";\n",
        "```\n",
        "\n",
        "If you want to get automated tracing of your model calls you can also set your [LangSmith](https://docs.smith.langchain.com/) API key by uncommenting below:\n",
        "\n",
        "```typescript\n",
        "// process.env.LANGSMITH_TRACING=\"true\"\n",
        "// process.env.LANGSMITH_API_KEY=\"your-api-key\"\n",
        "```"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "93df377e",
      "metadata": {},
      "source": [
        "## Instantiation\n",
        "\n",
        "Make sure your index has the same dimension count as your embeddings. The default for OpenAI `text-embedding-3-small` is 1536."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 1,
      "id": "dc37144c-208d-4ab3-9f3a-0407a69fe052",
      "metadata": {
        "tags": []
      },
      "outputs": [],
      "source": [
        "import { UpstashVectorStore } from \"@langchain/community/vectorstores/upstash\";\n",
        "import { OpenAIEmbeddings } from \"@langchain/openai\";\n",
        "\n",
        "import { Index } from \"@upstash/vector\";\n",
        "\n",
        "const embeddings = new OpenAIEmbeddings({\n",
        "  model: \"text-embedding-3-small\",\n",
        "});\n",
        "\n",
        "const indexWithCredentials = new Index({\n",
        "  url: process.env.UPSTASH_VECTOR_REST_URL,\n",
        "  token: process.env.UPSTASH_VECTOR_REST_TOKEN,\n",
        "});\n",
        "\n",
        "const vectorStore = new UpstashVectorStore(embeddings, {\n",
        "  index: indexWithCredentials,\n",
        "  // You can use namespaces to partition your data in an index\n",
        "  // namespace: \"test-namespace\",\n",
        "});"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "afa53a9c",
      "metadata": {},
      "source": [
        "## Usage with built-in embeddings\n",
        "\n",
        "To use the built-in Upstash embeddings, you can pass a `FakeEmbeddings` instance to the `UpstashVectorStore` constructor. This will make the `UpstashVectorStore` use the built-in embeddings, which you selected when creating the index."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "id": "cbabe6f7",
      "metadata": {},
      "outputs": [],
      "source": [
        "import { UpstashVectorStore } from \"@langchain/community/vectorstores/upstash\";\n",
        "import { FakeEmbeddings } from \"@langchain/core/utils/testing\";\n",
        "\n",
        "import { Index } from \"@upstash/vector\";\n",
        "\n",
        "const indexWithEmbeddings = new Index({\n",
        "  url: process.env.UPSTASH_VECTOR_REST_URL,\n",
        "  token: process.env.UPSTASH_VECTOR_REST_TOKEN,\n",
        "});\n",
        "\n",
        "const vectorStore = new UpstashVectorStore(new FakeEmbeddings(), {\n",
        "  index: indexWithEmbeddings,\n",
        "});"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "ac6071d4",
      "metadata": {},
      "source": [
        "## Manage vector store\n",
        "\n",
        "### Add items to vector store"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 2,
      "id": "17f5efc0",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[ '1', '2', '3', '4' ]\n"
          ]
        }
      ],
      "source": [
        "import type { Document } from \"@langchain/core/documents\";\n",
        "\n",
        "const document1: Document = {\n",
        "  pageContent: \"The powerhouse of the cell is the mitochondria\",\n",
        "  metadata: { source: \"https://example.com\" }\n",
        "};\n",
        "\n",
        "const document2: Document = {\n",
        "  pageContent: \"Buildings are made out of brick\",\n",
        "  metadata: { source: \"https://example.com\" }\n",
        "};\n",
        "\n",
        "const document3: Document = {\n",
        "  pageContent: \"Mitochondria are made out of lipids\",\n",
        "  metadata: { source: \"https://example.com\" }\n",
        "};\n",
        "\n",
        "const document4: Document = {\n",
        "  pageContent: \"The 2024 Olympics are in Paris\",\n",
        "  metadata: { source: \"https://example.com\" }\n",
        "}\n",
        "\n",
        "const documents = [document1, document2, document3, document4];\n",
        "\n",
        "await vectorStore.addDocuments(documents, { ids: [\"1\", \"2\", \"3\", \"4\"] });"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "dcf1b905",
      "metadata": {},
      "source": [
        "**Note:** After adding documents, there may be a slight delay before they become queryable.\n",
        "\n",
        "### Delete items from vector store"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 3,
      "id": "ef61e188",
      "metadata": {},
      "outputs": [],
      "source": [
        "await vectorStore.delete({ ids: [\"4\"] });"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "c3620501",
      "metadata": {},
      "source": [
        "## Query vector store\n",
        "\n",
        "Once your vector store has been created and the relevant documents have been added you will most likely wish to query it during the running of your chain or agent. \n",
        "\n",
        "### Query directly\n",
        "\n",
        "Performing a simple similarity search can be done as follows:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 7,
      "id": "aa0a16fa",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "* The powerhouse of the cell is the mitochondria [{\"source\":\"https://example.com\"}]\n",
            "* Mitochondria are made out of lipids [{\"source\":\"https://example.com\"}]\n"
          ]
        }
      ],
      "source": [
        "const filter = \"source = 'https://example.com'\";\n",
        "\n",
        "const similaritySearchResults = await vectorStore.similaritySearch(\"biology\", 2, filter);\n",
        "\n",
        "for (const doc of similaritySearchResults) {\n",
        "  console.log(`* ${doc.pageContent} [${JSON.stringify(doc.metadata, null)}]`);\n",
        "}"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "3ed9d733",
      "metadata": {},
      "source": [
        "See [this page](https://upstash.com/docs/vector/features/filtering) for more on Upstash Vector filter syntax.\n",
        "\n",
        "If you want to execute a similarity search and receive the corresponding scores you can run:"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 8,
      "id": "5efd2eaa",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "* [SIM=0.576] The powerhouse of the cell is the mitochondria [{\"source\":\"https://example.com\"}]\n",
            "* [SIM=0.557] Mitochondria are made out of lipids [{\"source\":\"https://example.com\"}]\n"
          ]
        }
      ],
      "source": [
        "const similaritySearchWithScoreResults = await vectorStore.similaritySearchWithScore(\"biology\", 2, filter)\n",
        "\n",
        "for (const [doc, score] of similaritySearchWithScoreResults) {\n",
        "  console.log(`* [SIM=${score.toFixed(3)}] ${doc.pageContent} [${JSON.stringify(doc.metadata)}]`);\n",
        "}"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "0c235cdc",
      "metadata": {},
      "source": [
        "### Query by turning into retriever\n",
        "\n",
        "You can also transform the vector store into a [retriever](/docs/concepts/retrievers) for easier usage in your chains. "
      ]
    },
    {
      "cell_type": "code",
      "execution_count": 9,
      "id": "f3460093",
      "metadata": {},
      "outputs": [
        {
          "name": "stdout",
          "output_type": "stream",
          "text": [
            "[\n",
            "  Document {\n",
            "    pageContent: 'The powerhouse of the cell is the mitochondria',\n",
            "    metadata: { source: 'https://example.com' },\n",
            "    id: undefined\n",
            "  },\n",
            "  Document {\n",
            "    pageContent: 'Mitochondria are made out of lipids',\n",
            "    metadata: { source: 'https://example.com' },\n",
            "    id: undefined\n",
            "  }\n",
            "]\n"
          ]
        }
      ],
      "source": [
        "const retriever = vectorStore.asRetriever({\n",
        "  // Optional filter\n",
        "  filter: filter,\n",
        "  k: 2,\n",
        "});\n",
        "await retriever.invoke(\"biology\");"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "e2e0a211",
      "metadata": {},
      "source": [
        "### Usage for retrieval-augmented generation\n",
        "\n",
        "For guides on how to use this vector store for retrieval-augmented generation (RAG), see the following sections:\n",
        "\n",
        "- [Tutorials: working with external knowledge](/docs/tutorials/#working-with-external-knowledge).\n",
        "- [How-to: Question and answer with RAG](/docs/how_to/#qa-with-rag)\n",
        "- [Retrieval conceptual docs](/docs/concepts/retrieval)"
      ]
    },
    {
      "cell_type": "markdown",
      "id": "8a27244f",
      "metadata": {},
      "source": [
        "## API reference\n",
        "\n",
        "For detailed documentation of all `UpstashVectorStore` features and configurations head to the [API reference](https://api.js.langchain.com/classes/langchain_community_vectorstores_upstash.UpstashVectorStore.html)."
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "TypeScript",
      "language": "typescript",
      "name": "tslab"
    },
    "language_info": {
      "codemirror_mode": {
        "mode": "typescript",
        "name": "javascript",
        "typescript": true
      },
      "file_extension": ".ts",
      "mimetype": "text/typescript",
      "name": "typescript",
      "version": "3.7.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}
